﻿<template>
	<div class="cRMCustomer-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="" :close-on-click-modal="false"
		center fullscreen
		>
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="客户名称" prop="custName">
							<el-input v-model="ruleForm.custName" placeholder="请输入客户名称" maxlength="64" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="客户负责人" prop="custLeadId">
							<el-input-number v-model="ruleForm.custLeadId" placeholder="请输入客户负责人" clearable />
						</el-form-item>
					</el-col> -->

					<!-- <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="是否关注" prop="isCollect">
							<el-input-number v-model="ruleForm.isCollect" placeholder="请输入是否关注" clearable />
						</el-form-item>
					</el-col> -->
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="客户来源" prop="customerSource">
							<el-select  v-model="ruleForm.customerSource" placeholder="请输入客户来源" show-word-limit clearable >
								<el-option label="抖音" :value="1" />
								<el-option label="快手" :value="2" />
								<el-option label="小红书" :value="3" />
								<el-option label="百度搜索" :value="4" />
								<el-option label="广告" :value="5" />
								<el-option label="转介绍" :value="6" />
								<el-option label="线上询价" :value="7" />
								<el-option label="预约上门" :value="8" />
								<el-option label="陌拜" :value="9" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="首要联系人" prop="contactName">
							<el-input v-model="ruleForm.contactName" placeholder="请输入首要联系人" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="手机号码" prop="mobilePhone">
							<el-input v-model="ruleForm.mobilePhone" placeholder="请输入手机号码" maxlength="11" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="电话号码" prop="telephone">
							<el-input v-model="ruleForm.telephone" placeholder="请输入电话号码" maxlength="11" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="客户级别" prop="customerLevel">
							<!-- <el-input-number v-model="ruleForm.customerLevel" placeholder="请输入客户级别" clearable /> -->
							<el-select  v-model="ruleForm.customerLevel" placeholder="请输入客户级别" show-word-limit clearable >
								<el-option label="重点" :value="1" />
								<el-option label="普通" :value="2" />
								<el-option label="非优先客户" :value="3" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="客户状态" prop="customerStatus">
							<!-- <el-input-number v-model="ruleForm.customerStatus" placeholder="请输入客户状态" clearable /> -->
							<el-select  v-model="ruleForm.customerStatus" placeholder="请输入客户状态" show-word-limit clearable >
								<el-option label="跟进中" :value="1" />
								<el-option label="有意向" :value="2" />
								<el-option label="无回复" :value="3" />
								<el-option label="无意向" :value="4" />
								<el-option label="已成交" :value="5" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="客户规格" prop="customerSpecification">
							<!-- <el-input-number v-model="ruleForm.customerSpecification" placeholder="请输入客户规格" clearable /> -->
							<el-select  v-model="ruleForm.customerSpecification" placeholder="请输入客户规格" show-word-limit clearable >
								<el-option label="初创企业" :value="1" />
								<el-option label="中小企业" :value="2" />
								<el-option label="大型企业" :value="3" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="下次联系时间" prop="nextContactDate">
							<el-date-picker v-model="ruleForm.nextContactDate" type="date" placeholder="下次联系时间" />
						</el-form-item>
					</el-col>
				
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="客户地址" prop="districtList">
							<el-cascader  maxlength="255" show-word-limit clearable
								v-model="ruleForm.districtList"
								:options="resData.regionTreeData"
								:props="{ value: 'id', label: 'name', expandTrigger: 'hover' ,checkStrictly: true  }"
								class="w100">
							</el-cascader>
							<el-form-item v-show="false">
								<el-input v-model="ruleForm.province" />
								<el-input v-model="ruleForm.city" />
							</el-form-item>
						</el-form-item>
					</el-col>

					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="详细地址" prop="detailedAddress">
							<el-input  v-model="ruleForm.detailedAddress" placeholder="请输入详细地址" maxlength="255" show-word-limit clearable />
						</el-form-item>
					</el-col>
				
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="排序" prop="orderNo">
							<el-input-number v-model="ruleForm.orderNo" placeholder="请输入排序" clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" :offset="6" class="mb15">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="ruleForm.remark" placeholder="请输入备注"  rows="5" maxlength="2000" show-word-limit clearable />
						</el-form-item>
					</el-col>

				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
  	import { formatDate } from '/@/utils/formatTime';
	import { addCRMCustomer, updateCRMCustomer, detailCRMCustomer,getRegionTree } from "/@/api/main/cRMCustomer";

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	//自行添加其他规则
	const rules = ref<FormRules>({
		custName: [{required: true, message: '请输入客户名称！', trigger: 'blur',},],
		customerSource: [{required: true, message: '请输入客户来源！', trigger: 'blur',},],
	});


	const resData = ref({
		regionTreeData: [] as any,
	});

	// 页面加载时
	onMounted(async () => {
		var resRegin = await getRegionTree(0);
		resData.value.regionTreeData = resRegin.data.result ?? [];
	});

	    // 打开弹窗
		const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id) {

			var resDetail = (await detailCRMCustomer(rowData.id)).data.result;
			ruleForm.value = resDetail;

			//地区
			if (resDetail.province != null && resDetail.province != 0) {
				ruleForm.value.districtList = [resDetail.province]
			}
			if (resDetail.city != null && resDetail.city != 0) {
				ruleForm.value.districtList = [resDetail.province, resDetail.city]
			}
			if (resDetail.district != null && resDetail.district != 0) {
				ruleForm.value.districtList = [resDetail.province, resDetail.city, resDetail.district]
			}
		}
		else
			ruleForm.value = rowData;
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addCRMCustomer(values);
				} else {
					await updateCRMCustomer(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




